package com.qf.commons.data.result;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author: tiger
 * @Data: 2024/10/22 22:14
 * @Description: 统一返回前端的对象 TODO -还有很多种类去完善
 **/

@Data
public class AjaxResult<T> implements Serializable {

    private int code;        //默认必备
    private String msg;      //默认必备
    private T data;


    /**
     * @MethodName: success
     * @Param: [data]
     * @Return: com.qf.commons.data.result.AjaxResult<S>
     * @Date: 2024/10/22 22:29
     * @Description: 返回成功的对象 -不带数据
     **/
    public static <S> AjaxResult<S> success() {
        AjaxResult<S> result = new AjaxResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

    /**
     * @MethodName: success
     * @Param: [data]
     * @Return: com.qf.commons.data.result.AjaxResult<S>
     * @Date: 2024/10/22 22:29
     * @Description: 返回成功的对象 -带数据
     **/
    public static <S> AjaxResult<S> success(S data) {
        AjaxResult<S> result = new AjaxResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    /**
     * @MethodName: fail
     * @Param: [resultCode]
     * @Return: com.qf.commons.data.result.AjaxResult<S>
     * @Date: 2024/10/22 22:32
     * @Description: 返回失败的对象 -不带数据
     **/
    public static <S> AjaxResult<S> fail(int code, String msg) {
        AjaxResult<S> result = new AjaxResult<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * @MethodName: fail
     * @Param: [resultCode]
     * @Return: com.qf.commons.data.result.AjaxResult<S>
     * @Date: 2024/10/22 22:32
     * @Description: 返回失败的对象 -不带数据
     **/
    public static <S> AjaxResult<S> fail(ResultCode resultCode) {
        AjaxResult<S> result = new AjaxResult<>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        return result;
    }

    /**
     * @MethodName: fail
     * @Param: [resultCode, data]
     * @Return: com.qf.commons.data.result.AjaxResult<S>
     * @Date: 2024/10/22 22:32
     * @Description: 返回失败的对象 -带数据的
     **/
    public static <S> AjaxResult<S> fail(ResultCode resultCode, S data) {
        AjaxResult<S> result = new AjaxResult<>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        result.setData(data);
        return result;
    }

}
